import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.JTextArea;

public class gui {

	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					gui window = new gui();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public gui() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 466, 306);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.NORTH);

		JButton createDB = new JButton("Create");
		createDB.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String db_name = JOptionPane.showInputDialog("Database Name:");
				String r = JOptionPane
						.showInputDialog("Please enter the attributes:");
				String rs = JOptionPane
						.showInputDialog("Please enter the null-free subschema attributes:");
				String fd = JOptionPane
						.showInputDialog("Please enter the set of standard FDs:");
				CreateSampleDB cr = new CreateSampleDB();
				if (cr.createDB(db_name, r, rs, fd)) {
					JOptionPane.showMessageDialog(frame, "SUCCESSFUL");
				}
			}
		});
		panel.add(createDB);

		JButton viewDB = new JButton("View");
		viewDB.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					String command = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe http://localhost/phpmyadmin/index.php?db=fyp&token=d913c3076bf20d09732d561eeb37be79";
					Runtime.getRuntime().exec(command);
				} catch (Exception ex) {
					System.out.println("cannot execute command. " + ex);
				}

			}
		});
		panel.add(viewDB);

		final JTextArea att = new JTextArea();

		att.setEditable(true);
		frame.getContentPane().add(att, BorderLayout.WEST);

		final JTextArea nfs = new JTextArea();
		frame.getContentPane().add(nfs, BorderLayout.CENTER);

		final JTextArea fd = new JTextArea();
		frame.getContentPane().add(fd, BorderLayout.EAST);

		final JTextArea sampled = new JTextArea();
		frame.getContentPane().add(sampled, BorderLayout.SOUTH);

		JButton verifyDB = new JButton("Verify");
		verifyDB.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String db_name = JOptionPane
						.showInputDialog("Enter DB name you want to check:");
				Validator val = new Validator(db_name);

				// Attribute display
				att.append("List of attributes:\n");
				ArrayList<String> attributes = val.getAttributes();
				for (int i = 0; i < attributes.size(); i++) {
					att.append(attributes.get(i) + "\n");
				}

				// NFS display
				nfs.append("List of Nullfree subschema:\n");
				ArrayList<String> nf_sub = val.getNullfreeSubschema();
				for (int i = 0; i < nf_sub.size(); i++) {
					nfs.append(nf_sub.get(i) + "\n");
				}

				// FD display
				fd.append("List of detected FDs:\n");
				ArrayList<FuncDep> fds = val.getFDs();
				for (int i = 0; i < fds.size(); i++) {
					fd.append(fds.get(i).toString() + "\n");
				}

				sampled.append("Sampled Generated DB");
				ArrayList<String[]> sample = val.getGeneratedSampleDB();
				for (int i = 0; i < sample.size(); i++) {
					for (int j = 0; j < sample.get(i).length; j++) {
						sampled.append(sample.get(i)[j] + " ");
					}
					sampled.append("\n");
				}

				sampled.append(val.validate());
				// JOptionPane.showMessageDialog(frame, val.validate());
			}
		});
		panel.add(verifyDB);
	}

}
